# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=suitesparse5
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-suitesparse5")
pkgver=5.13.0
pkgrel=1
pkgdesc='A suite of sparse matrix algorithms (mingw-w64)'
url="https://faculty.cse.tamu.edu/davis/suitesparse.html"
license=('spdx:LGPL-3.0-or-later AND BSD-3-Clause AND LGPL-2.1-or-later AND GPL-2.0-or-later AND LGPL-2.0-or-later AND Apache-2.0 AND GPL-3.0-or-later')
arch=('any')
mingw_arch=('mingw32' 'clang32')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/DrTimothyAldenDavis/SuiteSparse/archive/v${pkgver}.tar.gz"
        "0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch"
        "0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch"
        "0003-mingw-w64-no-CUDA.patch"
        "0004-mingw-w64-install-static-libs.patch"
        "0005-suitesparse-5.8.1-skip-building-Mongoose-GraphBLAS.patch"
        "0006-suitesparse-5.8.1-fix-mp-link-order.patch")
provides=("${MINGW_PACKAGE_PREFIX}-suitesparse=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-suitesparse")
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-metis")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
noextract=(${_realname}-${pkgver}.tar.gz)
options=('staticlibs')
sha256sums=('59c6ca2959623f0c69226cf9afb9a018d12a37fab3a8869db5f6d7f83b6b147d'
            'fa2ec5c66213c6ab15b68c5a2f846786649db524b050dfafb083a21628f8d348'
            '130e0d7261f8f05e23ca92495f4f120182725392297eb6ef10d2d51408a54131'
            '8a94c54322323773442c2c60f54795d402d64ac126e2a1a67e48a0075f8571fa'
            'e0faab259d5c474c6af51c9b471f645810a308a1c83976275b1969e81e7d8390'
            '8628100f41800e0e4238a8f949fb99e8667e0b58208130606881612d7172835e'
            '1cf6cd948e5178cbcb16bb0d3c97829311250dedda1e9dcbe0c85dd6bdc4461e')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}
  rm -rf SuiteSparse-${pkgver}
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}"/SuiteSparse-${pkgver}

  apply_patch_with_msg \
    0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch \
    0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch \
    0003-mingw-w64-no-CUDA.patch \
    0004-mingw-w64-install-static-libs.patch \
    0005-suitesparse-5.8.1-skip-building-Mongoose-GraphBLAS.patch \
    0006-suitesparse-5.8.1-fix-mp-link-order.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -rf "${srcdir}/SuiteSparse-${pkgver}" "${srcdir}/build-${MSYSTEM}"

  cd "${srcdir}/build-${MSYSTEM}"

  # This avoids needing to use a very large patch (we still get diffs but
  # that is down to a bug in format string handling somewhere where-by an
  # extra 0-padding value is emitted in exponents of scientific floats).
  [[ -d "${PWD}/bin" ]] || mkdir "${PWD}/bin"
  echo "#!/usr/bin/env bash"                         > "${PWD}/bin/diff"
  echo "/usr/bin/diff --strip-trailing-cr \"\$@\""  >> "${PWD}/bin/diff"

  # This avoids needing to use another very large patch.
  [[ -d "${PWD}/lib" ]] || mkdir "${PWD}/lib"
  ar cru "${PWD}/lib/librt.a"

  PATH="${PWD}/bin:${PATH}" \
  CC=${CC} \
  CXX=${CXX} \
  CFLAGS="-DLONGBLAS='long long'" \
  CXXFLAGS="-DLONGBLAS='long long'" \
  LDFLAGS="-L${PWD}/lib" \
  MY_METIS_LIB=-lmetis \
  BLAS=-lopenblas \
  LAPACK=-lopenblas \
  CFOPENMP="-fopenmp" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    make CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/{bin,lib,include} || true

  cd "${srcdir}/build-${MSYSTEM}"

  PATH=${PWD}/bin:"${PATH}" \
  CC=${CC} \
  CXX=${CXX} \
  CFLAGS="-DLONGBLAS='long long'" \
  CXXFLAGS="-DLONGBLAS='long long'" \
  MY_METIS_LIB=-lmetis \
  BLAS=-lopenblas \
  LAPACK=-lopenblas \
  CFOPENMP="-fopenmp" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  make install \
    INSTALL="${pkgdir}${MINGW_PREFIX}" \
    DESTDIR="${pkgdir}" \
    CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}
